﻿using MiniExcelLibs.Attributes;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AttendanceSysytem.Models
{
    /// <summary>
    /// 从Excel导入员工请假的实体类型
    /// </summary>
    public class ImportLeaveRecord
    {

        [ExcelColumnIndex("E")]
        public string EmployeeName { get; set; }
        [ExcelColumnIndex("F")]
        public string Depart_Text { get; set; }
        [ExcelColumnIndex("G")]
        public string Account { get; set; }
        /// <summary>
        /// 请假类型 病假、调休、年假、事假
        /// </summary>
        [ExcelColumnIndex("H")]
        public string LeaveTypeText { get; set; }
        [ExcelColumnIndex("I")]
        public DateTime BeginTime {  get; set; }
        [ExcelColumnIndex("J")]
        public DateTime EndTime { get; set; }
        [ExcelColumnIndex("N")]
        public string ApproveStatusText { get; set; }

        /// <summary>
        /// 用作判断条件时需要先判断LeaveHour,否则不生效
        ///  dateKey - DayOption
        /// </summary>
        [ExcelIgnore()]
        public Dictionary<string, LeaveDayInfo> DayInfos = new Dictionary<string, LeaveDayInfo>();


        /// <summary>
        /// 离开小时数
        /// </summary>
        public double LeaveHour
        {
            get { return GetLeaveHour(); }
        }

        /// <summary>
        /// 审批状态 1 通过 2撤销
        /// </summary>
        public int ApproveStatus { 
            get {
                return ApproveStatusText switch
                {
                    "已通过" => 1,
                    "已撤销" => 2,
                    _ => 0
                };
            } 
        }

        public LeaveType LeaveType
        {
            get 
            {
                return LeaveTypeText switch
                {
                    "病假" => LeaveType.Sick,
                    "调休" => LeaveType.Lieu,
                    "年假" => LeaveType.Annual,
                    "事假" => LeaveType.Affairs,
                    "丧假" => LeaveType.Bereavement,
                    _ => LeaveType.None
                };
            }
        }

        private double GetLeaveHour()
        {
            DateTime date = this.BeginTime;
            int day = 0; //休假所占天数 ，从1开始
           
            while (date< EndTime)
            {
                double hour = 0; // 请假时间
                DayOption option = DayOption.FullDay;
                LeaveDayInfo leaveDayInfo = new LeaveDayInfo();
                day++;
                var tempEnd = this.EndTime;
                string dateKey = date.ToString("yyyy-MM-dd");
                var normalWorkStartTime = date.ToString("yyyy-MM-dd 08:30:00").ObjToDate();
                var normalWorkEndTime = date.ToString("yyyy-MM-dd 17:30:00").ObjToDate();
                if (date.DayOfYear != EndTime.DayOfYear) 
                    tempEnd = normalWorkEndTime;
                // 饭点时间是 11:30 -  12:30
                var lunchBegin = date.ToString("yyyy-MM-dd 11:30:00").ObjToDate();
                var lunchEnd = date.ToString("yyyy-MM-dd 12:30:00").ObjToDate();

                int offsethour = 0;
                if (date <= lunchBegin && tempEnd >= lunchEnd)
                    offsethour = -1;

                //判断请假范围
                if (date <= normalWorkStartTime && tempEnd >= normalWorkEndTime)
                    option = DayOption.FullDay;
                else if (date <= normalWorkStartTime && tempEnd <= lunchEnd)
                    option = DayOption.Moring;
                else if (date >= lunchBegin && tempEnd >= normalWorkEndTime)
                    option = DayOption.Afternoon;
                else
                    option = DayOption.Hour;


                



                var ts = tempEnd - date;
                
                hour += ts.Hours;
                if (ts.Minutes >= 30)
                    hour += 0.5;
                hour += offsethour;

                if (hour < 0)
                    hour = 0;
                leaveDayInfo.Hour = hour;
                leaveDayInfo.DayOption = option;
                leaveDayInfo.DateKey = dateKey;
                if (!DayInfos.ContainsKey(dateKey))
                    DayInfos.Add(dateKey, leaveDayInfo);

                //计算下一天的起始时间
                date = date.AddDays(1).ToString("yyyy-MM-dd 08:30:00").ObjToDate();
            }
            return DayInfos.Values.Sum(a=>a.Hour);
        }

    }

    public enum LeaveType:int
    {
        None = 0,
        /// <summary>
        /// 病假
        /// </summary>
        Sick,
        /// <summary>
        /// 调休
        /// </summary>
        Lieu,
        /// <summary>
        /// 年假
        /// </summary>
        Annual,
        /// <summary>
        /// 事假
        /// </summary>
        Affairs,
        /// <summary>
        /// 丧假
        /// </summary>
        Bereavement

    }
    /// <summary>
    /// 请假在一天中的范围
    /// </summary>
    public enum DayOption:int
    {
        /// <summary>
        /// 仅几个小时
        /// </summary>
        Hour = 0,
        /// <summary>
        /// 全天
        /// </summary>
        FullDay,
        /// <summary>
        /// 上午请假
        /// </summary>
        Moring ,
        /// <summary>
        /// 下午请假
        /// </summary>
        Afternoon
    }

    public class LeaveDayInfo
    {
        /// <summary>
        /// 请假日期
        /// </summary>
        public string DateKey {  get; set; }
        /// <summary>
        /// 请假范围
        /// </summary>
        public DayOption DayOption {  get; set; }
        /// <summary>
        /// 请假时间
        /// </summary>
        public double Hour {  get; set; }
    }


}
